package cn.wolfcode.wolf2world.redis.service.impl;

import cn.wolfcode.wolf2world.domain.Strategy;
import cn.wolfcode.wolf2world.domain.UserInfo;
import cn.wolfcode.wolf2world.exception.LogicException;
import cn.wolfcode.wolf2world.myenum.RedisKeysEnum;
import cn.wolfcode.wolf2world.redis.service.StaticRedisService;
import cn.wolfcode.wolf2world.redis.service.UserRedisService;
import cn.wolfcode.wolf2world.service.StrategyService;
import cn.wolfcode.wolf2world.utils.DateUtil;
import cn.wolfcode.wolf2world.vo.StaticRedisVO;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;
import java.util.*;

/**
 * @author 杨兴健
 * @Date 2020/8/7 21:12
 */
@Service
@Slf4j
public class StaticRedisServiceImpl implements StaticRedisService {



    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private StrategyService strategyService;


    /**
     * 阅读数+1
     * @param id
     */
    @Override
    public void addViewnum(String id) {
        //获取vo
        StaticRedisVO vo = this.getStaticRedisVO(id);
        //阅读数+1
        vo.setViewnum(vo.getViewnum() + 1);
        //更新vo到redis
        this.setStaticRedisVO(vo);

    }

    /**
     * 评论数+1
     * @param strategyId
     */
    @Override
    public void addComment(String strategyId) {
        //获取vo
        StaticRedisVO vo = this.getStaticRedisVO(strategyId);
        //回复数+1
        vo.setReplynum(vo.getReplynum()+1);
        //更新到redis中
        this.setStaticRedisVO(vo);
    }

    /**
     * 顶操作
     * @param uid
     * @param sid
     * @return
     */
    @Override
    public boolean clickThumbup(String uid, String sid) {
        //拼接key
        String key = RedisKeysEnum.STRATEGY_THUMBSUP_SID_UID.join(sid, uid);
        //判断key是否存在
        if (redisTemplate.hasKey(key)) {
            //存在，今天已顶过，不做任何事情
            return false;
        }
        //不存在，今天没顶过，获取vo，点赞数加1
        StaticRedisVO staticRedisVO = this.getStaticRedisVO(sid);
        staticRedisVO.setThumbsupnum(staticRedisVO.getThumbsupnum() + 1);
        //保存vo
        this.setStaticRedisVO(staticRedisVO);
        //保存key 设置过期时间为今天剩余时间
        Long expiredTime = DateUtil.getSurplusTime();
        redisTemplate.opsForValue().set(key,"1",Duration.ofSeconds(expiredTime));
        return true;
    }

    /**
     * 获取vo
     * @param id
     * @return
     */
    @Override
    public StaticRedisVO getStaticRedisVO(String id) {
        //拼接key
        String key = RedisKeysEnum.STRATEGY_STATIC_VO.join(id);
        StaticRedisVO vo = new StaticRedisVO();
        //判断key是否存在
        if (!redisTemplate.hasKey(key)) {
            //不存在，数据库中获取数据
            Strategy strategy = strategyService.get(id);
            BeanUtils.copyProperties(strategy,vo);
            vo.setSid(id);
            //更新vo
            this.setStaticRedisVO(vo);

        }else {
            //获取redis中的数据
            String s = redisTemplate.opsForValue().get(key);
            vo = JSON.parseObject(s, StaticRedisVO.class);
        }
        return vo;
    }

    /**
     * 更新vo
     * @param vo
     */
    @Override
    public void setStaticRedisVO( StaticRedisVO vo) {
        String key = RedisKeysEnum.STRATEGY_STATIC_VO.join(vo.getSid());
        redisTemplate.opsForValue().set(key,JSON.toJSONString(vo));
    }


    /**
     * 判断key是否存在
     * @param id
     * @return
     */
    @Override
    public boolean hasKey(String id) {
        //拼接key
        String key = RedisKeysEnum.STRATEGY_STATIC_VO.join(id);
        return redisTemplate.hasKey(key);
    }

    /**
     * 获取redis所有攻略的数据统计
     * @return
     */
    @Override
    public List<StaticRedisVO> getRedisStrategy() {
        //拼接key
        String key = RedisKeysEnum.STRATEGY_STATIC_VO.join("*");
        Set<String> keys = redisTemplate.keys(key);
        List<StaticRedisVO> list = new ArrayList<>();
        //遍历每个key
        for (String k : keys) {
            String value = redisTemplate.opsForValue().get(k);
            list.add(JSON.parseObject(value,StaticRedisVO.class));
        }
        return list;
    }

}
